class SpecParser {
	private static List filterByTag(String tag, List lines){
		boolean isBegin = false
		boolean isEnd = true

		List ll = []
		for(one in lines){
			if(one == ';' + tag){
				isBegin = true
				isEnd = false

				continue
			}

			// next
			if(one.startsWith(';') && isBegin){
				isBegin = false
				isEnd = true

				continue
			}

			if(isBegin && !isEnd)
				ll << one
		}

		ll
	}
	private static Map parseKeyValue(String tag, List lines){
		def ll = filterByTag(tag, lines)
		toKeyValue(ll)
	}

	public static Map toKeyValue(List ll){
		def one = [:]
		
		for(line in ll){
			def arr = line.split(':')
			one[arr[0].trim()] = arr.size() == 2 ? arr[1].trim() : arr[1..-1].join(':').trim()
		}

		one
	}

	private static void parseTags(List tags, Map item, List lines){
		for(tag in tags){
			item[tag] = tag.contains('=') ? parseKeyValue(tag, lines) : filterByTag(tag, lines)
			if(tag == 'json')
				item[tag] = item[tag].join('')
		}
	}

	static void setup(Map item, List lines){
		parseTags(['sql', 'sqlFile', 'requestFile'], item, lines)
	}

	static void teardown(Map item, List lines){
		parseTags(['sql', 'sqlFile', 'requestFile'], item, lines)
	}

	static void headers(Map item, List lines){
		def one = toKeyValue(lines)
		one.each{k, v ->
			item[k] = v
		}
	}

	static void params(Map item, List lines){
		parseTags(['json', 'key=value'], item, lines)
	}

	static void expects(Map item, List lines){
		item.asserts = lines
	}
}
